package com.start.handler;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.start.bean.User3;

@Controller
@RequestMapping("/user")
public class Response {

	@RequestMapping("/testAjax")
	@ResponseBody
	public User3 testAjax(@RequestBody User3 user) {
		System.out.println("Response.testAjax()");
		System.out.println(user);
		user.setAge(110);
		return user;
	}

//	@RequestMapping("/testAjax")
//	// @ResponseBody
//	public void testAjax(@RequestBody String user, HttpServletResponse response) {
//		System.out.println("Response.testAjax()");
//		System.out.println(user);
//		try {
//			PrintWriter out = response.getWriter();
//			out.write("成功了");
//			out.flush();
//			out.close();
//		} catch (IOException e) {
//			System.out.println("失败了");
//			e.printStackTrace();
//		}
//	}

	/**
	 * ForwardOrRedirect:转发或重定向
	 * @return
	 */
	@RequestMapping("/ForwardOrRedirect")
	public String testForwardOrRedirect() {
		System.out.println("Response.ForwardOrRedirect()");

		return "forward:/user/ModelAndView";
		// return "redirect:/user/ModelAndView";
	}

	/**
	 * 返回值是ModelAndView类型
	 */
	@RequestMapping("/ModelAndView")
	public ModelAndView testModelAndView() {
		System.out.println("Response.testModelAndView()");
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("successResponse");
		modelAndView.addObject("bb", "张三");
		return modelAndView;
	}

	/**
	* 返回值是void类型
	*/
	@RequestMapping("/testVoid")
	public void testVoid() {
		System.out.println("Response.testVoid()");
	}

	/**	
	 * 返回值是String类型
	 * @return
	 */
	@RequestMapping("/testString")
	public String testString() {
		System.out.println("Response.testString()");
		return "successResponse";
	}

}
